import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../common.dart';

class Resale extends StatefulWidget {
  Resale({super.key});

  @override
  State<Resale> createState() => _WebDetailState();
}

class _WebDetailState extends State<Resale> {
  final piliang = Get.parameters['piliang'] ?? 0;

  List<dynamic> result = [];
  final thumb = ''.obs;
  final id = [].obs;
  final title = ''.obs;
  final nftId = ''.obs;
  final minratio = ''.obs;
  final maxratio = ''.obs;

  final brokerageratio = ''.obs;
  final editiontaxratio = ''.obs;

  final sellnum = ''.obs;

  RxString money = ''.obs;
  RxString pay = ''.obs;

  late TextEditingController _controller;
  late TextEditingController controller;

  String? uuid;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    controller = TextEditingController();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (piliang == '1') {
        result = json.decode(Get.parameters['arr'] ?? ''); // [4, 1]

        uuid = Get.parameters['uuid'];
        getData(url: '/packages/info/$uuid').then((ress) {
          if (ress != null) {
            var res = ress['info'];
            title.value = res['title'];
            nftId.value = '${res['nftId']}';
            minratio.value = '${res['examine_min_ratio']}';
            maxratio.value = '${res['examine_max_ratio']}';
            sellnum.value = '${res['sell_num']}';
            editiontaxratio.value = '${res['edition_tax_ratio']}';
            brokerageratio.value = '${res['brokerage_ratio']}';
            thumb.value = res['thumb'];
          }
        });
      } else {
        id.add(Get.parameters['id']!);
        title.value = Get.parameters['title']!;
        nftId.value = Get.parameters['nftId']!;
        minratio.value = Get.parameters['examine_min_ratio']!;
        maxratio.value = Get.parameters['examine_max_ratio']!;
        sellnum.value = Get.parameters['sell_num']!;
        editiontaxratio.value = Get.parameters['edition_tax_ratio']!;
        brokerageratio.value = Get.parameters['brokerage_ratio']!;
        thumb.value = Get.parameters['img']!;
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: buildBackButton(context),
        backgroundColor: Color(0xffffffff),
        title: wText(text: '寄售', size: 18),
        centerTitle: true,
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          children: [
            Expanded(
              child: ListView(
                children: [
                  Divider(color: Color(0XFFDDDDDD), height: 30),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(5.0), // 设置圆角大小
                        child: Obx(
                          () => Image.network(
                            '$thumb',
                            width: 80,
                            height: 80,
                            fit: BoxFit.fill,
                            errorBuilder: (context, error, stackTrace) {
                              return SizedBox(width: 80);
                            },
                          ),
                        ),
                      ),
                      SizedBox(width: 10),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Obx(
                            () => Text(
                              '$title',
                              style: TextStyle(
                                fontSize: 15,
                                color: Color(0XFF0F172B),
                              ),
                            ),
                          ),

                          SizedBox(height: 20),
                          Row(
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '限量',
                                    style: TextStyle(
                                      fontSize: 11,
                                      color: Color(0XFF999999),
                                    ),
                                  ),
                                  Obx(
                                    () => Text(
                                      '$sellnum',
                                      style: TextStyle(
                                        fontSize: 14,
                                        color: Color(0XFF0F172B),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              SizedBox(width: 40),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '藏品编号',
                                    style: TextStyle(
                                      fontSize: 11,
                                      color: Color(0XFF999999),
                                    ),
                                  ),
                                  Obx(
                                    () =>
                                        piliang == '1'
                                            ? Text(
                                              '#${hao('$nftId')}...',
                                              style: TextStyle(
                                                fontSize: 14,
                                                color: Color(0XFF0F172B),
                                              ),
                                            )
                                            : Text(
                                              '#${hao('$nftId')}',
                                              style: TextStyle(
                                                fontSize: 14,
                                                color: Color(0XFF0F172B),
                                              ),
                                            ),
                                  ),
                                ],
                              ),
                              SizedBox(width: 40),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '获取方式',
                                    style: TextStyle(
                                      fontSize: 11,
                                      color: Color(0XFF999999),
                                    ),
                                  ),
                                  Text(
                                    '购买',
                                    style: TextStyle(
                                      fontSize: 14,
                                      color: Color(0XFF0F172B),
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
                  Divider(color: Color(0XFFDDDDDD), height: 30),
                  Row(
                    children: [
                      Text(
                        '寄售价格',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF999999),
                        ),
                      ),
                      Expanded(
                        child: TextField(
                          onChanged: (vals) {
                            if (vals.isNotEmpty) {
                              num numericValue = num.parse(
                                brokerageratio.value,
                              );
                              num taxratio = num.parse(editiontaxratio.value);

                              num val = num.parse(vals);
                              num v = (numericValue + taxratio) / 100;

                              pay.value = (val * v).toStringAsFixed(2);

                              money.value = (val - val * v).toStringAsFixed(2);
                            } else {
                              money.value = "0";
                              pay.value = "0";
                            }

                            // (val * (100 - numericValue) / 100)
                          },
                          controller: _controller,
                          textAlign: TextAlign.end,
                          style: TextStyle(fontSize: 14),
                          decoration: InputDecoration(
                            isCollapsed: true, //设置高度
                            hintStyle: TextStyle(color: Color(0XFF0F172B)),
                            border: InputBorder.none, // 隐藏输入框默认边框
                            hintText: '0.00', // 提示文本
                          ),
                        ),
                      ),
                      Text(
                        '元',
                        style: TextStyle(
                          fontSize: 12,
                          color: Color(0XFF999999),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 3),
                  Obx(
                    () => Text(
                      '寄售价必须大于$minratio元或小于$maxratio元',
                      style: TextStyle(fontSize: 11, color: Color(0XFF999999)),
                      textAlign: TextAlign.end,
                    ),
                  ),
                  SizedBox(height: 15),
                  Row(
                    children: [
                      Text(
                        '版权费与平台服务费',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF999999),
                        ),
                      ),
                      Expanded(
                        child: Obx(
                          () => Text(
                            '-$pay',
                            style: TextStyle(
                              fontSize: 12,
                              color: Color(0XFF0F172B),
                              fontWeight: FontWeight.bold,
                            ),
                            textAlign: TextAlign.end,
                          ),
                        ),
                      ),
                      Text(
                        '元',
                        style: TextStyle(
                          fontSize: 12,
                          color: Color(0XFF999999),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 20),
                  Row(
                    children: [
                      Text(
                        '实际到账',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF999999),
                        ),
                      ),
                      Expanded(
                        child: Obx(
                          () => Text(
                            '$money',
                            style: TextStyle(
                              fontSize: 12,
                              color: Color(0XFF0F172B),
                              fontWeight: FontWeight.bold,
                            ),
                            textAlign: TextAlign.end,
                          ),
                        ),
                      ),
                      Text(
                        '元',
                        style: TextStyle(
                          fontSize: 12,
                          color: Color(0XFF999999),
                        ),
                      ),
                    ],
                  ),
                  Divider(color: Color(0XFFDDDDDD), height: 50),
                  Text(
                    '说明',
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(height: 10),
                  Text(
                    '1、转出时请注意核对信息，藏品一旦转出将无法找回',
                    style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                  ),
                  SizedBox(height: 5),
                  Obx(
                    () => Text(
                      '2、交易成功后，将扣除$editiontaxratio%的版权费以及$brokerageratio%的平台服务费',
                      style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                    ),
                  ),
                  SizedBox(height: 20),
                ],
              ),
            ),
            Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      if (_controller.text.isNotEmpty) {
                        Get.dialog(
                          AlertDialog(
                            backgroundColor: Colors.transparent,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(5),
                            ),
                            content: Stack(
                              children: [
                                Image.asset(
                                  'static/tan.png',
                                  width: 255,
                                  fit: BoxFit.fitWidth,
                                ),
                                Positioned(
                                  top: 80,
                                  right: 0,
                                  left: 0,
                                  child: Column(
                                    children: [
                                      Text(
                                        '请输入交易密码',
                                        style: TextStyle(
                                          fontSize: 24,
                                          color: Color(0XFF0F172B),
                                        ),
                                      ),
                                      SizedBox(height: 30),
                                      Container(
                                        margin: EdgeInsets.symmetric(
                                          horizontal: 30,
                                        ),
                                        decoration: BoxDecoration(
                                          color: Color(0XFFF8F9FD),
                                          borderRadius: BorderRadius.circular(
                                            10,
                                          ),
                                        ),
                                        child: TextField(
                                          controller: controller,
                                          obscureText: true,
                                          style: TextStyle(fontSize: 15),
                                          decoration: InputDecoration(
                                            isCollapsed: true, //设置高度
                                            contentPadding: EdgeInsets.all(15),
                                            hintStyle: TextStyle(
                                              color: Color(0xff999999),
                                            ),
                                            border:
                                                InputBorder.none, // 隐藏输入框默认边框
                                            hintText: '输入交易密码', // 提示文本
                                          ),
                                        ),
                                      ),
                                      SizedBox(height: 30),
                                      GestureDetector(
                                        onTap: () {
                                          print(result);
                                          print(id);
                                          if (controller.text.isNotEmpty) {
                                            postData(
                                              url: '/packages/resales',
                                              data: {
                                                "package_sku_id":
                                                    piliang == '1'
                                                        ? result
                                                        : id,
                                                "level2password":
                                                    controller.text,
                                                "metaAddress": "",
                                                "money_price": _controller.text,
                                              },
                                            ).then((res) {
                                              if (res != null) {
                                                Get.offNamed('/');
                                                showTost('发布成功');
                                              }
                                            });
                                          } else {
                                            showTost('请输交易密码');
                                          }
                                        },
                                        child: Container(
                                          width: 210,
                                          height: 45,
                                          decoration: BoxDecoration(
                                            color: Color(0XFF0F172B),
                                            borderRadius: BorderRadius.circular(
                                              45,
                                            ),
                                          ),
                                          alignment: Alignment.center,
                                          child: Text(
                                            '确认',
                                            style: TextStyle(
                                              fontSize: 18,
                                              color: Color(0XFFFFFFFF),
                                            ),
                                          ),
                                        ),
                                      ),
                                      SizedBox(height: 10),
                                      GestureDetector(
                                        onTap: () {
                                          Get.back();
                                        },
                                        child: Text(
                                          '取消',
                                          style: TextStyle(
                                            fontSize: 18,
                                            color: Color(0XFF0F172B),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      } else {
                        showTost('请输价格');
                      }
                    },
                    child: Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.symmetric(vertical: 10),
                      height: 60,
                      decoration: BoxDecoration(
                        color: Color(0XFF0F172B),
                        borderRadius: BorderRadius.circular(60),
                      ),
                      child: Text(
                        '发布寄售信息',
                        style: TextStyle(
                          fontSize: 18,
                          color: Color(0XFFFFFFFF),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
